from langchain_openai import ChatOpenAI
from loguru import logger
from openai import OpenAI
from openai.types.chat import ChatCompletionMessageParam
from typing import Iterable
from app.conf import conf

chat_zhipu = ChatOpenAI(
    model=conf.llm_zhipu_model,
    temperature=conf.llm_zhipu_temperature,
    api_key=conf.llm_zhupu_apikey,
    base_url=conf.llm_zhupu_baseurl,
)


deepseek = OpenAI(
    base_url=conf.llm_deepseek_baseurl,
    api_key=conf.llm_deepseek_apikey,
)


def deepseek_chat(messages: Iterable[ChatCompletionMessageParam]):
    completion = deepseek.chat.completions.create(
        model=conf.llm_deepseek_model_chat, messages=messages, stream=True
    )

    for chunk in completion:
        if chunk.choices:
            delta = chunk.choices[0].delta
            if delta.content is not None and delta.content != "":
                logger.debug("[DS] yield token: {}", delta.content)
                yield delta.content
